package com.tuotu.gps;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.WindowManager;

import com.tuotu.gps.utils.Preference;

import java.lang.ref.WeakReference;

public class LogoActivity extends AppCompatActivity {

    private static final int MSG_START_LOGIN=0;
    private static final int MSG_START_MAIN=1;

    private SharedPreferences userPreferences;
    private MyHandler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //去除状态栏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_logo);
        init();
    }

    private void init() {
        mHandler=new MyHandler(this);
        userPreferences=getSharedPreferences(Preference.User.PREFERENCE_USER,MODE_PRIVATE);
        boolean status=userPreferences.getBoolean(Preference.User.PREFERENCE_USER_STATUS,false);
        if(!status){
            mHandler.sendEmptyMessageDelayed(MSG_START_LOGIN,2000);
        }else {
            mHandler.sendEmptyMessageDelayed(MSG_START_MAIN,2000);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(null!=mHandler)
            mHandler.removeCallbacksAndMessages(null);
    }

    private static class MyHandler extends  Handler{
        private final WeakReference<LogoActivity> reference;
        public MyHandler(LogoActivity activity){
            reference=new WeakReference<LogoActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(null==reference.get()){
                return;
            }
            LogoActivity activity=reference.get();
            switch (msg.what){
                case MSG_START_LOGIN:
                    activity.startActivity(new Intent(activity,LoginActivity.class));
                    activity.finish();
                    break;
                case MSG_START_MAIN:
                    activity.startActivity(new Intent(activity,RecordActivity2.class));
                    activity.finish();
                    break;
            }
        }
    }
}
